package com.mobile;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Utility {
	
	public static void printHello() {
		System.out.println("Hello");
	}
	
 
	public static String GetDate2() {
		Date now = new Date();
		Format formatter = new SimpleDateFormat("dd-MM-yyyy");
		String s = formatter.format(now);
		return s;
	}

	public static String GetTodayFromLastLogin(String content) {
		if (content != null) {
			return content.split(" ")[0];
		}
		return null;
	}

	public static String GetTenDayAgo(String today) {
		if (today != null) {
			Format formatter = new SimpleDateFormat("dd-MM-yyyy");
			Format formatter2 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = (Date) formatter.parseObject(today);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_YEAR, -9);
				Date newDate = calendar.getTime();
				return formatter2.format(newDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return null;
			}
		}
		return null;
	}

	public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md;
		md = MessageDigest.getInstance("MD5");
		byte[] md5hash = new byte[32];
		md.update(text.getBytes("iso-8859-1"), 0, text.length());
		md5hash = md.digest();
		return convertToHex(md5hash);
	}

	private static String convertToHex(byte[] data) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < data.length; i++) {
			int halfbyte = (data[i] >>> 4) & 0x0F;
			int two_halfs = 0;
			do {
				if ((0 <= halfbyte) && (halfbyte <= 9))
					buf.append((char) ('0' + halfbyte));
				else
					buf.append((char) ('a' + (halfbyte - 10)));
				halfbyte = data[i] & 0x0F;
			} while (two_halfs++ < 1);
		}
		return buf.toString();
	}

//	/*
//	 * this function that check whether Internet Connection or not
//	 */
//	public static boolean CheckInternet(Context c) {
//		ConnectivityManager connec = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
//		android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//		android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//		if (wifi.isConnected()) {
//			return true;
//		} else if (!mobile.isConnected()) {
//			return false;
//		} else if (mobile.isConnected()) {
//			return true;
//		}
//		return false;
//	}

	public static String getData1(String str) {
		StringBuilder content = new StringBuilder();

		try {
			URL url = new URL(str);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			while ((str = in.readLine()) != null) {
				content.append(str + "\n");
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		/*try {
			URL url = new URL(str);
			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

				int i;
				try {
					i = in.read();
					while (i != -1) {
						byteArrayOutputStream.write(i);
						i = in.read();
					}
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				return byteArrayOutputStream.toString();
			}*/

			return content.toString();

		/*} catch (SAXException e) {
			e.printStackTrace();
		}*/
	}
	public static String getData(String str) {
//		StringBuilder content = new StringBuilder();

		/*try {
			URL url = new URL(str);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			while ((str = in.readLine()) != null) {
				content.append(str + "\n");
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		ByteArrayOutputStream byteArrayOutputStream = null;

		try {
			URL url = new URL(str);
			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();
				 byteArrayOutputStream = new ByteArrayOutputStream();

				int i;
				try {
					i = in.read();
					while (i != -1) {
						byteArrayOutputStream.write(i);
						i = in.read();
					}
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				
			}

//			return content.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return byteArrayOutputStream.toString();
	}
	
//	public static AlertDialog.Builder createConfirmDialog(Context context, String title, String msg, DialogInterface.OnClickListener yesButtonClickListener, DialogInterface.OnClickListener noButtonClickListener)
//	{
//		AlertDialog.Builder dlg = new AlertDialog.Builder(context);
//		
//		dlg.setTitle(title);
//		dlg.setMessage(msg);
//		
//		dlg.setPositiveButton(context.getString(R.string.yes), yesButtonClickListener);
//		dlg.setNeutralButton(context.getString(R.string.no), noButtonClickListener);
//		
//		dlg.setCancelable(false);
//		
//		return dlg;
//	}
}
